<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

class Street extends CI_Controller {
	
	function __construct() {
		parent::__construct ();
	
	}
	
	public function index() {
		$eID = (int)$this->input->get('id');
		
		if(!isset($eID) || $eID == ''){
			$this->load->view('error');
			return ;
		}
		$this->db->where('id',$eID);
		$street = $this->db->get('street')->row_array();
		if(!isset($eID) || $street == null){
			$this->load->view('error');
			return ;
		}
		$eData = array ();
		$eData ['eID'] = $eID;
		$eData['street'] = $street;
		
		//街道下的小店
		$this->db->where('street',$eID);
		$this->db->limit(5,0);
		$eData['storeList'] = $this->db->get('store')->result_array();
		//街道下的图片
		$this->db->where('type',2);
		$this->db->where('belongId',$eID);
		$this->db->limit(3);
		$eData['photoList'] = $this->db->get('picture')->result_array();
		
		//街道下的话题
		$this->db->where('type',1);
		$this->db->where('belongId',$eID);
		$this->db->limit(6,0);
		$eData['topicList'] = $this->db->get('topic')->result_array();
		
		//街道下的公告
		$this->db->where('type',1);
		$this->db->where('belongId',$eID);
		$this->db->limit(7,0);
		$eData['announceList'] = $this->db->get('announce')->result_array();
		
		//标签
		$this->db->where('type',1);
		$this->db->where('belongId',$eID);
		$eData['tagList'] = $this->db->get('tag')->result_array();
		
		// 居住在这条街上的人数
		$this->db->where ( 'street', $eID );
		$eData ['lives'] = $this->db->count_all_results ( 'livestreet' );
		
		//刚入住的小店
		$this->db->where('street',$eID);
		$this->db->limit(6,0);
		$this->db->order_by('createtime', 'DESC');
		$eData['storeList'] = $this->db->get('store')->result_array();
		
		//我是否住在这里
		$eData['lived'] = false;
		$member = $this->session->userdata ( 'member' );
		if($member != NULL){
			$this->db->where('member',$member['id']);
			$this->db->where('street',$eID);
			$livestreets = $this->db->count_all_results('livestreet');
			if($livestreets > 0){
				$eData['lived'] = true;
			}
		}
		
		
		$this->load->model ( 'codevalue_model' );
		$eData ['area'] = $this->codevalue_model->getCodeValue ( 'area', $street['area']);
		$this->load->view ( 'street', $eData );
	}
	
  	function livehere() {
		if ($this->checklogin ()) {
			$streetid = $this->input->post('streetid');
			$member = $this->session->userdata ( 'member' );
			
			$eData = array();
			$eData['member'] = $member['id'];
			$eData['street'] = $streetid;
			$this->db->insert('livestreet',$eData);			
			header ( "Content-Type: text/html; charset=utf-8" );
			
			$ret ['success'] = 'true';
			$ret ['msg'] = '入住成功';
			echo json_encode ( $ret );
		} else {
			redirect ( 'member/tologin' );
		}
	
	}
	
	 function checklogin() {
		$member = $this->session->userdata ( 'member' );
		if ($member == null) {
			return false;
		} else {
			return true;
		}
	}
	
}

